import {
    createHashRouter,
    createRoutesFromElements,
    Route,
  } from "react-router-dom";
  
  import AuthRoute from "./AuthRouter";
  import configs from './RouterConfig'
  
  import { RouteConfig } from "./types";
  
   // 处理我们的routers
   const RouteAuthFun = (
    (routeList: RouteConfig[]) => {
      return routeList.map(
        (item) => {
          return (
            <Route
              path={item.path}
              lazy={item.lazy}
              element={ item.element &&
                <AuthRoute auth={item.auth} key={item.path}>
                  {item.element}
                </AuthRoute>
              }
              key={item.path}
            >
              {/* 递归调用，因为可能存在多级的路由 */}
              {item?.children && RouteAuthFun(item.children)}
            </Route>
          );
        }
      );
    }
  );
  
  
  function AppRouter() {
    console.log("app router");
    const router = createHashRouter(
      createRoutesFromElements(
        <>        
         {RouteAuthFun(configs)}
        </>
      )
    );
    return router;
  }
  
  export default AppRouter;
  